<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  news
 */

class m_news_actions_block_front_NewsAction extends f_core_Action
{
	public function execute()
	{
		$blockParams = $this->getBlockContext()->getParameters();
		$newsService = m_news_services_ItemService::getInstance();
        $docProvider = $this->getDocumentProvider();

		if(isset($blockParams["folderNewsId"]) && !empty($blockParams["folderNewsId"]))
		{
            $folder = $docProvider->getByModelAndUniqueId("modules_backoffice/folder", $blockParams["folderNewsId"], $blockParams['blockLang']);

            $suffix = $this->getRequestParameter("urlSuffix");

			if(!is_null($suffix) && $suffix['model'] == 'news/item')
			{
                $preview = false;
                if(!is_null($this->getRequestParameter('preview')) && $this->getRequestParameter('preview') == '1' && $this->userHasRight('backoffice_accessBo'))
                {
                    $preview = true;
                }
                
                $url = $suffix['url'];

                $tempDocService = m_backoffice_services_TempDocService::getInstance();

                if($preview)
                {
                     if($tempDocService->hasTemporary($id, $blockParams["blockLang"]))
                     {
                         $news[0] = $tempDocService->getTemporaryDocument($id, $blockParams["blockLang"]);
                     }
                }
                else
                {
                    $news[0] = $docProvider
                                    ->loadDocuments("modules_news/item")
                                    ->isChildOf($blockParams["folderNewsId"], "item", false)
                                    ->whereIsEqual("url", $url)
                                    ->byPublicationStatus("PUBLISHED")
                                    ->whereIsPublicationDatesValid()
                                    ->retrieveFirstDocument($blockParams["blockLang"]);
                }
                
                if(is_null($news[0]))
                {
                    $page = $this->getContext("currentPage")->setRedirect("404");
                    $this->updateContext("frontOffice", $page);
                }
                else
                {

                    if($news[0]->getPublicationStatus() != 'PUBLISHED')
                    {
                        if($this->getRequestParameter('preview') != '1' || !$this->getUserContext()->hasRight("backoffice_accessBo"))
                        {
                            $page = $this->getContext("currentPage")->setRedirect("404");
                            $this->updateContext("frontOffice", $page);
                            return '';
                        }
                    }

                    $order_number = f_relation_Manager::getInstance()->getOrderNumber($folder, $news[0], "item", $news[0]->getLang());

                    $news[1] = $docProvider
                                    ->loadDocuments("modules_news/item")
                                    ->isChildOf($blockParams["folderNewsId"], "item", true)
                                    ->whereIsLessThan("relation_order", $order_number)
                                    ->addDescOrder("relation_order")
                                    ->byPublicationStatus("PUBLISHED")
                                    ->whereIsPublicationDatesValid()
                                    ->retrieveFirstDocument($blockParams["blockLang"]);

                    $news[2] = $docProvider
                                    ->loadDocuments("modules_news/item")
                                    ->isChildOf($blockParams["folderNewsId"], "item", true)
                                    ->whereIsMoreThan("relation_order", $order_number)
                                    ->addAscOrder("relation_order")
                                    ->byPublicationStatus("PUBLISHED")
                                    ->whereIsPublicationDatesValid()
                                    ->retrieveFirstDocument($blockParams["blockLang"]);

                    $page = $this->getContext("currentPage")->setMetaTitle($news[0]->getLabel());
                    $this->updateContext("currentPage", $page);                   
                    if(!isset($blockParams['view'])) $blockParams['view'] = "Success";
                    $this->setView($blockParams['view'].'Detail');
                }
			}
			else
			{
				$news = $this->getDocumentProvider()
								->loadDocuments("modules_news/item")
								->isChildOf($blockParams["folderNewsId"], "item", true)
								->byPublicationStatus("PUBLISHED")
                                ->addDescOrder("document_startpublicationdate")
                                ->addAscOrder("relation_order")
                                ->whereIsPublicationDatesValid()
								->retrieveDocuments($blockParams["blockLang"]);

                if($this->getContext('indexation') == true)
                {
                    return '';
                }
                
                if(!isset($blockParams['view'])) $blockParams['view'] = "Success";

				$this->setView($blockParams['view'].'List');
			}

			$newsTab = $newsService->getNewsArray($news, $this->getContext("currentPage"));
        }

        $this->addToModel("page", $this->getContext("currentPage"));

        if(!empty($newsTab))
        {
            $this->addToModel("news", $newsTab);
            $this->addToModel("blockParams", $blockParams);
        }
	}	
	
	public function validate()
	{
		return true;
	}
	
	public function errorHandler()
	{
		$this->setView('error');
	}
}